const readlindSync = require('readline-sync');
module.exports ={
   studentinfor,
   scoreCheck,
   inputidcheck
}
//judge
function studentinfor(){
   console.log('请输入学生信息（格式：姓名, 学号, 民族, 班级, 学科: 成绩, ...），按回车提交：');
   let studentString = readlindSync.question();
   let studentArray = studentString.split(',');
   let studentObject = {};
   if(studentString[0]){studentObject.name = studentArray[0];}
   studentObject.id = idCheck(studentArray[1]);
   if(studentString[2]){studentObject.ethnic = studentArray[2]};
   studentObject.klass = klassCheck(studentArray[3]);
   let scoreArray = studentArray.slice(4); 
   studentObject.score = scoreCheck(scoreArray);
   return studentObject ;
}
//学号判断
function idCheck(id){
   if(!(/^\d{10}$/).test(id)){
           console.log("学号为十位数字！");
           return studentinfor().id;
   }else{
       return id;
   }
}
//班级判断
function klassCheck(klass){
   if(!(/^\d{6}$/).test(klass)){
       console.log("班级为6位数！");
       return studentinfor().klass;
   }else{
       return klass;
   }
}
//成绩判断
function scoreCheck(scoreArray){
   let score = {Math:'0',Chi:'0',Eng:'0',Code:'0'};
   for(let item of scoreArray){
       if((/^\w{1,}[:]\d{1,3}$/).test(item)){
           item = item.split(":");
           if(item[0] === "Math")score.Math = item[1];
           if(item[0] === "Chi")score.Chi = item[1];
           if(item[0] === "Eng")score.Eng = item[1];
           if(item[0] === "Code")score.Code = item[1];
       }else{
           console.log("成绩输入错误");
           return studentinfor().score;
       }
   }
   return score;
}
//成绩查询学号输入判断
function inputidcheck(inputid){
   let studentIdArray = inputid.split(',');
   for(let item of studentIdArray){
       if(!(/^\d{10}$/).test(item)){
           console.log('请按正确的格式输入要打印的学生的学号（格式： 学号, 学号,...）');
           return false;
       }else{
           return true;
       }
   }
   return 0 ;
}
